<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

   <xsl:variable name="space" select="' '" />
   
   <xsl:variable name="DATATYPE_INT">Int</xsl:variable>
   <xsl:variable name="DATATYPE_SMALLINT">Smallint</xsl:variable>
   <xsl:variable name="DATATYPE_TINYINT">Tinyint</xsl:variable>
   <xsl:variable name="DATATYPE_BIGINT">Bigint</xsl:variable>
   <xsl:variable name="DATATYPE_VARCHAR">Varchar</xsl:variable>
   <xsl:variable name="DATATYPE_CHAR">Char</xsl:variable>
   <xsl:variable name="DATATYPE_TEXT">Text</xsl:variable>
   <xsl:variable name="DATATYPE_IDENTITY">Identity</xsl:variable>
   <xsl:variable name="DATATYPE_FLOAT">Float</xsl:variable>
   <xsl:variable name="DATATYPE_MONEY">Money</xsl:variable>
   <xsl:variable name="DATATYPE_DECIMAL">Decimal</xsl:variable>
   <xsl:variable name="DATATYPE_BOOLEAN">Bit</xsl:variable>
   <xsl:variable name="DATATYPE_GUID">Uniqueidentifier</xsl:variable>
   <xsl:variable name="DATATYPE_DATETIME">DateTime</xsl:variable>

   <xsl:variable name="OPERATIONTYPE_GREATERTHAN">GreaterThan</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_GREATERTHANOREQUAL">GreaterThanOrEqual</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_LESSTHAN">LessThan</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_LESSTHANOREQUAL">LessThanOrEqual</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_EQUAL">Equal</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_INCLUSIVEBETWEEN">InclusiveBetween</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_EXCLUSIVEBETWEEN">ExclusiveBetween</xsl:variable>
   <xsl:variable name="OPERATIONTYPE_IN">In</xsl:variable>

   <xsl:variable name="MODIFIERTYPE_DEFAULT">Default</xsl:variable>
   <xsl:variable name="MODIFIERTYPE_NOT">Not</xsl:variable>

   <xsl:variable name="OBJECTTYPE_INTERSECTION">intersection</xsl:variable>

   <xsl:variable name="INDEXTYPE_PRIMARY">Primary</xsl:variable>
   <xsl:variable name="INDEXTYPE_FOREIGN">Foreign</xsl:variable>
   <xsl:variable name="INDEXTYPE_CLUSTERED">Clustered</xsl:variable>

   <xsl:variable name="METHODTYPE_GET">Get</xsl:variable>
   <xsl:variable name="METHODTYPE_CREATE">Create</xsl:variable>
   <xsl:variable name="METHODTYPE_UPDATE">Update</xsl:variable>
   <xsl:variable name="METHODTYPE_LIST">List</xsl:variable>
   <xsl:variable name="METHODTYPE_CUSTOM">Custom</xsl:variable>

   <xsl:variable name="PARAMDIR_IN">in</xsl:variable>
   <xsl:variable name="PARAMDIR_OUT">out</xsl:variable>
   
   <xsl:template name="getFieldSize">
      <xsl:param name="field" />
      <xsl:choose>
         <xsl:when test="$field/@object">
            <xsl:call-template name="getFieldSizeByObject">
               <xsl:with-param name="field" select="$field/@field" />
               <xsl:with-param name="object" select="$field/@object" />
            </xsl:call-template>
         </xsl:when>
         <xsl:when test="$field/index[ @type = 'foreign' ]">
            <xsl:value-of select="//fields[ ( index/@type = 'primary' ) and ( position() = 1 ) ]/@size" />
         </xsl:when>
         <xsl:otherwise>
            <xsl:call-template name="getFieldSizeByObject">
               <xsl:with-param name="field" select="$field/@name" />
               <xsl:with-param name="object" select="ancestor::object/@name" />
            </xsl:call-template>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>   
   
   <xsl:template name="getFieldSizeByObject">
      <xsl:param name="field" />
      <xsl:param name="object" />
      <xsl:variable name="targetField" select="//object[ @name = $object ]/fields/field[ @name = $field ]" />
      <xsl:choose>
         <xsl:when test="$targetField/@type">
            <xsl:value-of select="$targetField/@size" />
         </xsl:when>
         <xsl:when test="$targetField/index[ @type = 'foreign' ]">
            <xsl:variable name="objectname" select="$targetField/index[ @type = 'foreign' ]/@object" />
            <xsl:value-of select="//object[ @name = $object ]/fields/field[ index/@type = 'primary' ]/@size" />
         </xsl:when>
      </xsl:choose>
   </xsl:template>      
   
   <xsl:template name="getSQLValue">
		<xsl:param name="pDataType" />
		<xsl:param name="pValue" />
		<xsl:choose>
			<xsl:when test="( $pDataType = $DATATYPE_VARCHAR ) or ( $pDataType = $DATATYPE_DATETIME ) or ( $pDataType = $DATATYPE_TEXT )">
				<xsl:text>'</xsl:text>
				<xsl:value-of select="$pValue" />
				<xsl:text>'</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$pValue" />
			</xsl:otherwise>
		</xsl:choose>
   </xsl:template>
   
</xsl:stylesheet>
